// pages/appointment/appointment.js
import {
  throttle
} from '../../utils/util'
import api from '../../utils/api' // 引入api接口管理文件
const app = getApp()
Page({
  onLoad: function (options) {
    let status = options.status * 1 || -100
    console.log(status, 'status===============')
    this.setData({
      typeChoose: status
    })
  },
  onShow() {
    this.setData({
      orderList: [],
      orderMyOrders: [],
      pageNum: 1,
      isEmpty: true,
      dataList: []
    }, () => {
      this.getList()
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    //     status:0  代付款
    // status：1   已付款
    // status：4   待收货
    // status：2   已完成 
    // status ：不传  全部
    appointmentList: [{
        status: -100,
        text: '全部'
      },
      {
        status: 0,
        text: '待付款'
      },
      {
        status: 1,
        text: '已付款'
      },
      {
        status: 4,
        text: '待收货'
      },
      {
        status: 2,
        text: '已完成'
      }
    ],
    typeChoose: -100,
    orderList: [],
    pageNum: 1,
    dataList: [],
    totalPage: 1,
    orderMyOrders: [],
    orderPage: 1,
    firstData: [],
    isEmpty: true
  },

  onReachBottom: function () {
    if (this.data.totalPage && this.data.pageNum + 1 <= this.data.totalPage * 1) {
      this.setData({
        pageNum: this.data.pageNum + 1
      }, () => {
        this.getList()
      })
    }
  },

  appointmentListTap(e) {
    let status = e.currentTarget.dataset.status
    console.log(e.currentTarget.dataset, 'status==================')
    this.setData({
      typeChoose: status,
      pageNum: 1,
      dataList: [],
      orderMyOrders: []
    }, () => {
      this.getList()
    })
  },
  moveTo(e) {
    wx.setStorage({
      key: "appointmentIndex",
      data: this.data.typeChoose
    })
    if (this.data.typeChoose * 1 === 0) {
      let orderItemId = e.currentTarget.dataset.item
      throttle(function (e) {
        wx.navigateTo({
          url: `../orderMessage/orderMessage?orderItemId=${orderItemId}&&type=1`
        })
      }, 2000)
    } else {
      let id = e.currentTarget.dataset.id
      let type = e.currentTarget.dataset.type * 1
      if (type === 1) {
        let orderItemId = e.currentTarget.dataset.item
        throttle(function (e) {
          wx.navigateTo({
            url: `../orderMessage/orderMessage?orderItemId=${orderItemId}&&type=1`
          })
        }, 2000)
      } else {
        throttle(function (e) {
          wx.navigateTo({
            url: `../orderMessage/orderMessage?id=${id}&&type=2`
          })
        }, 2000)
      }

    }
  },
  getList() {
    let status = this.data.typeChoose * 1
    if (status === -100) {
      status = ''
    }
    api.myOrdersV2({
      data: {
        pageSize: 10,
        pageNum: this.data.pageNum,
        mobile: wx.getStorageSync('mobile'),
        status: status
      },
      success: res => {
        if (res.code * 1 === 0) {
          let dataList = res.data.dataList

          dataList.forEach(item => {
            if (item.hasOwnProperty('vaccineOrderItems')) {
              item.vaccineOrderItems.forEach(children => {
                let arrangeStatus = children.arrangeStatus * 1
                switch (arrangeStatus) {
                  case 0:
                    children.arrangeStatusText = '排队中'
                    break
                  case 6:
                    children.arrangeStatusText = '待预约'
                    break
                  case 1:
                    children.arrangeStatusText = '预约中'
                    break
                  case 2:
                    children.arrangeStatusText = '预约中'
                    break
                  case 3:
                    children.arrangeStatusText = '预约中'
                    break
                  case 4:
                    children.arrangeStatusText = '已预约'
                    break
                  case 5:
                    children.arrangeStatusText = '已预约'
                    break
                  case -1:
                    children.arrangeStatusText = '已取消'
                    break
                }
                let refundStatus = children.refundStatus * 1
                switch (refundStatus) {
                  case -1:
                    children.refundStatusText = '退款失败'
                    break
                  case 1:
                    children.refundStatusText = '退款中'
                    break
                  case 2:
                    children.refundStatusText = '退款成功'
                    break
                }
              })
            }
          })

          this.setData({
            dataList: [...this.data.dataList, ...dataList],
            totalPage: res.data.totalPage,
            isEmpty: [...this.data.dataList, ...dataList].length === 0 ? true : false
          })
        }
      }
    })
  },
  payAppWeChatPay(e) {
    const that = this
    let item = e.currentTarget.dataset.item
    api.payAppWeChatPay({
      data: {
        payChannelId: 1,
        orderNo: item.orderNo, //订单号
        appid: "wxff3fc00494f4f0e3", //应用APPID
        body: item.orderItems[0]['productName'], //商品描述
        detail: "", //商品详情信息
        fee_type: "CNY", //币种，默认人民币
        limit_pay: "" //默认空，上传此参数no_credit--可限制用户不能使用信用卡支付
      },
      success: res => {
        if (res.code * 1 === 0) {
          wx.requestPayment({
            timeStamp: res.data.timeStamp + '',
            nonceStr: res.data.nonceStr,
            package: res.data.packageStr,
            signType: 'MD5',
            paySign: res.data.paySign,
            success(res) {
              that.setData({
                orderMyOrders: []
              })
              that.getList()
            },
            fail(fail) {
              console.log(fail, 'fail=======')
            }
          })
        }
      }
    })
  },
  moveToVaccine(e) {
    let {
      source,
      orderNo,
      healthExam
    } = e.currentTarget.dataset.item
    if (source && orderNo) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../registeredVaccine/registeredVaccine?source=${source}&orderNo=${orderNo}&healthExam=${healthExam}`
        })
      }, 2000)
    }
  },
  moveToUserAssess(e) {
    let {
      source,
      orderNo,
      healthExam
    } = e.currentTarget.dataset.item
    if (source && orderNo) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../userAssess/userAssess?source=${source}&orderNo=${orderNo}&healthExam=${healthExam}`
        })
      }, 2000)
    }
  },
  moveToAppraise(e) {
    let {
      id
    } = e.currentTarget.dataset.item
    app.globalData.userAppraise = e.currentTarget.dataset.item
    if (id) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../userAppraise/userAppraise?vaccineOrderId=${id}`
        })
      }, 2000)
    }
  },
  moveToAppraiseList() {
    throttle(function (e) {
      wx.navigateTo({
        url: `../appraiseList/appraiseList`
      })
    }, 2000)
  }
})